package com.chg.mymvc.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");

//      //4.判断登录信息对象是否为null
//      if(user==null){
//          //未登录
//           //5.再次判断，当前请求是否为放行白名单：允许未登录执行的操作（请求登录页面，登录操作）
//           String servletPath = request.getServletPath();
//          if(servletPath.endsWith("login.jsp") || servletPath.endsWith("LoginServlet")){
//               //是，直接放行
//               filterChain.doFilter(request, response);
//           }else{
//                //否，跳转到登录页面
//               response.sendRedirect("login.html");
//           }
//       }else{
//           //已登录，直接放行
         filterChain.doFilter(request, response);
//    }


    }

    @Override
    public void destroy() {

    }
}
